﻿package jokers{
	import flash.display.MovieClip;
	import flash.utils.*;
	import flash.geom.Point;

	public class JStateAnimation {
		private static const DELAY:int = 30;
		private static const SMALL:int = 10;
		private static const SHACK:int = 40;
		
		private static var id:uint = 0;
		
		//下落特效
		public static function gameWin():void{
			for(var y:int = 0 ; y < JMatrix.getInstance().height ; y++){
				for(var x:int = 0 ; x < JMatrix.getInstance().width ; x++){
					var mc:MovieClip = JMatrix.getInstance().getCrystal(x,y,false);
					
					if(mc == null) continue;
					
					var js:JStateAnimation = new JStateAnimation();
					js.setFallDown(mc);
					
					js.id = setInterval(js.fallDown,DELAY);
				}
			}
		}
		
		//缩小特效
		public static function smallAll():void{
			for(var y:int = 0 ; y < JMatrix.getInstance().height ; y++){
				for(var x:int = 0 ; x < JMatrix.getInstance().width ; x++){
					var mc:MovieClip = JMatrix.getInstance().getCrystal(x,y,false);
					
					if(mc == null) continue;
					
					var js:JStateAnimation = new JStateAnimation();
					js.setSmall(mc);
					
					js.id = setInterval(js.small,DELAY);
				}
			}
		}
		
		//颤抖特效
		public static function shackAll(func:Function = null):void{
			var js:JStateAnimation = new JStateAnimation();
			js.setShack(func);
			
			for(var y:int = 0 ; y < JMatrix.getInstance().height ; y++){
				for(var x:int = 0 ; x < JMatrix.getInstance().width ; x++){
					var mc:MovieClip = JMatrix.getInstance().getCrystal(x,y,false);
					if(mc == null) continue;
					var obj:Object = new Object();
					obj.mc = mc;
					obj.p = new Point();
					obj.p.x = mc.x;
					obj.p.y = mc.y;
					obj.desx = 3 - int(6*Math.random());
					obj.desy = -10 - 10*Math.random();
					
					js.ary_mc.push(obj);
				}
			}
			js.id = setInterval(js.shack,DELAY);
		}
		
		//----------------------------------------------------------------------------------------------
		private var ary_mc:Array = new Array();
		
		private var mc:MovieClip;
		private var id:uint;
		private var desx:int;
		private var desy:int;
		private var time:int;
		private var per:Number;
		private var position:Point;
		private var func:Function;
		
		//--------------------------------------------------颤抖
		public function setShack(func:Function = null):void{
			this.time = -SHACK;
			this.func = func;
		}
		
		public function shack():void{
			time++;
			
			if(time < 0){
				for each(var obj:Object in ary_mc){
					obj.mc.x = obj.p.x + 3 - int(6*Math.random());
					obj.mc.y = obj.p.y + 3 - int(6*Math.random());
				}
				
			}else if(time >= 0){
				var allFinish:Boolean = true;
				for each(obj in ary_mc){
					obj.desy += 2;
					obj.mc.y += obj.desy;
					obj.mc.x += obj.desx;
					
					if(obj.mc.y < JMatrix.HEIGHT) allFinish = false;
				}
				
				if(allFinish){
					for each(obj in ary_mc){
						JMatrix.getInstance().crystalStage.removeChild(obj.mc);
					}
					if(func != null) func(); 
					clearInterval(this.id);
					delete(this);
				}
			}
		}
		
		//--------------------------------------------------缩小
		public function setSmall(mc:MovieClip):void{
			this.mc = mc;
			this.time = SMALL;
			this.per = this.mc.scaleX;
		}
		
		public function small():void{
			mc.scaleX = time / SMALL * per;
			mc.scaleY = time / SMALL * per;
			mc.alpha = time / SMALL;
			
			time--;
				
			if(time < 0){
				JMatrix.getInstance().crystalStage.removeChild(mc);
				clearInterval(this.id);
				delete(this);
			}
		}
		
		//--------------------------------------------------下落
		public function setFallDown(mc:MovieClip):void{
			this.mc = mc;
			desy = Math.random()*15+5;
			time = Math.random()*20+40;
		}
		
		public function fallDown():void{
			mc.y += desy;
			
			time--;
			if(time < 20){
				mc.alpha = time / 20;
			}
			if(time < 0){
				JMatrix.getInstance().crystalStage.removeChild(mc);
				clearInterval(this.id);
				delete(this);
			}
		}
	}
}
